package com.LittleHan.crm.workbench.service;

import com.LittleHan.crm.exception.*;
import com.LittleHan.crm.settings.dao.UserDao;
import com.LittleHan.crm.utils.DateTimeUtil;
import com.LittleHan.crm.utils.UUIDUtil;
import com.LittleHan.crm.vo.PaginationVo;
import com.LittleHan.crm.workbench.dao.*;
import com.LittleHan.crm.workbench.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service(value = "clueServiceImpl")
public class ClueServiceImpl implements ClueService{
    @Autowired
    ClueDao clueDao;
    @Autowired
    UserDao userDao;
    @Autowired
    ClueRemarkDao clueRemarkDao;
    @Autowired
    ClueActivityRelationDao clueActivityRelationDao;
    @Autowired
    ActivityDao activityDao;
    @Autowired
    CustomerDao customerDao;
    @Autowired
    ContactsDao contactsDao;
    @Autowired
    CustomerRemarkDao customerRemarkDao;
    @Autowired
    ContactsRemarkDao contactsRemarkDao;
    @Autowired
    ContactsActivityRelationDao contactsActivityRelationDao;
    @Autowired
    TranDao tranDao;
    @Autowired
    TranHistoryDao tranHistoryDao;
    //保存clue信息
    @Override
    public int clueSave(Clue clue) throws ClueInsertException{
        int n=clueDao.insertClue(clue);
        if(n==0){
            throw new ClueInsertException("保存失败");
        }
        return n;
    }

    public PaginationVo<Clue> getTotalAndClueList(String fullname,String company,String phone,String source,
                                                  String owner,String mphone,String state,int pageNo,int pageSize){
        int num=clueDao.getTotal(fullname,company,phone,source,owner,mphone,state,pageNo,pageSize);
        List<Clue> list=clueDao.selectClue(fullname,company,phone,source,owner,mphone,state,pageNo,pageSize);
        PaginationVo<Clue> paginationVo=new PaginationVo<>();
        paginationVo.setDataList(list);
        paginationVo.setTotal(num);
        return paginationVo;
    }

    @Override
    public Clue clueGet(String id) {
        Clue clue=clueDao.getClue(id);
        return clue;
    }

    @Override
    public int clueUpdate(Clue clue) throws ClueUpdateException{
        int num=clueDao.updateClue(clue);
        if(num==0){
            throw new ClueUpdateException("更新失败");
        }
        return num;
    }

    @Override
    public int clueDel(String []id) {
        int num=clueDao.delClue(id);
        return num;
    }

    @Override
    public Clue getClueToInf(String id) {
        Clue clue=clueDao.clueToInf(id);
        return clue;
    }

    @Override
    public List<ClueRemark> getClueRemarkList(String id) {
        List<ClueRemark> list=clueRemarkDao.getClueRemarkList(id);
        return list;
    }

    @Override
    public int clueRemarkAdd(ClueRemark clueRemark) throws ClueRemarkAddException{
        int num=clueRemarkDao.addClueRemark(clueRemark);
        if(num==0){
            throw new ClueRemarkAddException("添加失败");
        }
        return num;
    }

    @Override
    public int clueRemarkDel(String id) throws ClueRemarkDelException {
        int num=clueRemarkDao.delClueRemark(id);
        if(num==0){
            throw new ClueRemarkDelException("删除失败");
        }
        return num;
    }

    @Override
    public int clueRemarkUpd(ClueRemark clueRemark) throws ClueRemarkUpdException {
        int num=clueRemarkDao.updateClueRemark(clueRemark);
        if(num==0){
            throw new ClueRemarkUpdException("更新失败");
        }
        return num;
    }
    //线索详细信息市场活动加载
    @Override
    public List<Map<String, Object>> clueRemarkActivity(String clueId) {
        List<Map<String, Object>> map=clueActivityRelationDao.getClueRemarkActivity(clueId);
        return map;
    }
    //线索详细信息市场活动解除关联


    @Override
    public int clueRemarkActivityRemove(String id) throws ClueRemarkActivityRemoveException{
        int num=clueActivityRelationDao.removeClueRemarkActivity(id);
        if(num==0){
            throw new ClueRemarkActivityRemoveException("解除关联失败");
        }
        return num;
    }

    @Override
    public List<Activity> clueRemarkActivitySearch(String name,String clueId) {
        List<Activity> list=clueActivityRelationDao.searchClueRemarkAddActivity(name,clueId);
        return list;
    }

    @Override
    public List<ClueActivityRelation> clueRemarkRel(String[] activityId, String clueId) throws ClueRemarkRelevancyException{
        int num=0;
        List<ClueActivityRelation> list=new ArrayList<>();
        for(String s:activityId){
            ClueActivityRelation cr=new ClueActivityRelation();
            cr.setId(UUIDUtil.getUUID());
            cr.setClueId(clueId);
            cr.setActivityId(s);
            list.add(cr);
            clueActivityRelationDao.clueRemarkRelevancy(cr);
            num++;
        }
        if(num!=activityId.length){
            throw new ClueRemarkRelevancyException("关联失败");
        }

        return list;
    }

    @Override
    public List<Activity> convertActivityGet(String name) {
        List<Activity> list=activityDao.getConvertActivity(name);
        return list;
    }

    @Override
    public String makeTransform(Tran tran, String clueId, String createBy) {
        Clue clue=clueDao.clueToInf(clueId);//获取线索 的信息
        String company=clue.getCompany();//线索
        Customer customer=customerDao.searchName(company);//获取客户信息
        if(customer==null){//如果没有就要新建客户
            customer=new Customer();
            customer.setId(UUIDUtil.getUUID());
            customer.setOwner(clue.getOwner());
            customer.setName(company);
            customer.setWebsite(clue.getWebsite());
            customer.setPhone(clue.getPhone());
            customer.setCreateBy(createBy);
            customer.setCreateTime(DateTimeUtil.getSysTime());
            customer.setContactSummary(clue.getContactSummary());
            customer.setNextContactTime(clue.getNextContactTime());
            customer.setDescription(clue.getDescription());
            customer.setAddress(clue.getAddress());
            customerDao.createCustomer(customer);
        }
        //添加联系人
        Contacts contacts=new Contacts();//联系人
        contacts.setId(UUIDUtil.getUUID());
        contacts.setSource(clue.getSource());
        contacts.setOwner(clue.getOwner());
        contacts.setNextContactTime(clue.getNextContactTime());
        contacts.setMphone(clue.getMphone());
        contacts.setJob(clue.getJob());
        contacts.setFullname(clue.getFullname());
        contacts.setEmail(clue.getEmail());
        contacts.setDescription(clue.getDescription());
        contacts.setCustomerId(customer.getId());
        contacts.setCreateTime(DateTimeUtil.getSysTime());
        contacts.setCreateBy(createBy);
        contacts.setContactSummary(clue.getContactSummary());
        contacts.setAppellation(clue.getAppellation());
        contacts.setAddress(clue.getAddress());
        contactsDao.addContacts(contacts);
        //线索备注转为客户备注以及联系人备注
        List<ClueRemark> list=clueRemarkDao.getClueRemarkListByClueId(clueId);//获取要删除的tbl_clue_remark表中id为clueId的
        clueRemarkDao.deleteClueRemark(clueId);//tbl_clue_remark表中删除id为clueId的
        for(ClueRemark c:list){//将线索备注放到客户备注和联系人备注中
            CustomerRemark crk=new CustomerRemark();
            crk.setId(UUIDUtil.getUUID());
            crk.setNoteContent(c.getNoteContent());
            crk.setCreateBy(c.getCreateBy());
            crk.setCreateTime(c.getCreateTime());
            crk.setEditFlag("0");
            crk.setCustomerId(customer.getId());
            int num=customerRemarkDao.addCustomerRemark(crk);

            ContactsRemark csk=new ContactsRemark();
            csk.setId(UUIDUtil.getUUID());
            csk.setNoteContent(c.getNoteContent());
            csk.setCreateBy(c.getCreateBy());
            csk.setCreateTime(c.getCreateBy());
            csk.setEditFlag("0");
            csk.setContactsId(customer.getId());
            System.out.println(csk);
            contactsRemarkDao.addContactsRemark(csk);
        }

        //线索和市场活动的关系转为联系人和市场活动的关系
        List<ClueActivityRelation> l=clueActivityRelationDao.getByClueId(clueId);
        for(ClueActivityRelation car:l){
            String activityId=car.getActivityId();
            ContactsActivityRelation contactsActivityRelation=new ContactsActivityRelation();
            contactsActivityRelation.setId(UUIDUtil.getUUID());
            contactsActivityRelation.setActivityId(activityId);
            contactsActivityRelation.setContactsId(contacts.getId());
            contactsActivityRelationDao.addContactsActivityRelation(contactsActivityRelation);
        }
        //如果有交易创建交易
        System.out.println("tran---"+tran);
        if(tran!=null){
            System.out.println("!!!!qqqqqqqwwwww!!!!!!");
            tran.setId(UUIDUtil.getUUID());
            tran.setCreateBy(createBy);
            tran.setCreateTime(DateTimeUtil.getSysTime());
            tran.setOwner(clue.getOwner());
            tran.setCustomerId(clue.getId());
            tran.setContactsId(contacts.getId());
            tran.setSource(clue.getSource());
            tran.setDescription(clue.getDescription());
            tran.setNextContactTime(clue.getNextContactTime());
            tran.setContactSummary(clue.getContactSummary());
            tranDao.addTran(tran);
            //创建交易的同时创建交易历史
            TranHistory tranHistory=new TranHistory();
            tranHistory.setId(UUIDUtil.getUUID());
            tranHistory.setStage(tran.getStage());
            tranHistory.setMoney(tran.getMoney());
            tranHistory.setExpectedDate(tran.getExpectedDate());
            tranHistory.setCreateTime(tran.getCreateTime());
            tranHistory.setCreateBy(createBy);
            tranHistory.setTranId(tran.getId());
            tranHistoryDao.addTranHistory(tranHistory);
        }
        //删除线索与市场活动关系
        for(ClueActivityRelation car:l){
            clueActivityRelationDao.removeClueRemarkActivity(car.getId());
        }
        //删除线索
            clueDao.delByClueId(clueId);
        return null;
    }
}
